package com.example.lightbulb;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.Switch;

public class MainActivity extends Activity {

	private SeekBar seekBar;
	private Switch switchButton;
	private ImageView lightBulbOnImage;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		initView();
		initHandler();
	}

	private void initView() {

		this.seekBar = (SeekBar) this.findViewById(R.id.seekBar1);
		this.switchButton = (Switch) this.findViewById(R.id.switch1);
		this.lightBulbOnImage = (ImageView) this
				.findViewById(R.id.lightBulbOnImage);

	}

	private void initHandler() {

		seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {

				int max = seekBar.getMax();
				int alpha = (int) ((double) progress / max * 255);
				lightBulbOnImage.setImageAlpha(alpha);
			}
		});

		switchButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {

				seekBar.setEnabled(isChecked);

				int visiable = (isChecked) ? View.VISIBLE : View.INVISIBLE;
				lightBulbOnImage.setVisibility(visiable);
			}
		});

		// default close
		switchButton.setChecked(false);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
